<template>
  <div class="norm">
    <div class="norm__title">商品规格</div>
    <div class="norm__list">
      <div class="norm__item" v-for="(item, index) in classArr" :key="index">
        <el-form ref="form" :model="item" :rules="normRules">
          <div class="norm__item__name">
            <div class="norm__item__name--left">
              <div>规格名称</div>
              <el-form-item prop="name">
                <el-input v-model="item.name" placeholder="规格名称" style="width: 200px; margin-left: 20px"></el-input>
              </el-form-item>
            </div>
            <el-button type="danger" class="norm__item__name--del" @click="delNormHandle(index)">删除规格</el-button>
          </div>
          <div class="norm__item__value">
            <div class="norm__item__value--mg34">规格值</div>
            <div class="norm__item__value__content">
              <Draggable v-model="item.children" @sort="specSortHandle">
                <el-tag :key="tagIndex" v-for="(tag, tagIndex) in item.children" closable :disable-transitions="false"
                  @close="handleClose(tag.name, index, tagIndex)">
                  {{ tag.name }}
                </el-tag>
              </Draggable>
              <el-input class="input-new-tag" v-if="item.inputVisble" v-model="item.inputValue" ref="saveTagInput"
                size="small" @keyup.enter.native="handleInputConfirm(index)" @blur="handleInputConfirm(index)">
              </el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInput(index)">添加新规格</el-button>
            </div>
          </div>
          <el-button size="small" type="primary" v-if="index === classArr.length - 1" @click="addNewClass">+添加规格项目
          </el-button>
        </el-form>
      </div>
      <el-button size="small" type="primary" v-if="classArr.length === 0" @click="addNewClass">+添加规格项目</el-button>
    </div>
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Prop } from "vue-property-decorator";
import { NormType } from "./goodNormType";
import Draggable from "vuedraggable";
@Component({
  components: {
    Draggable,
  },
})
export default class GoodsNorms extends Vue {
  @Prop()
  classArr;
  normRules = {
    name: [{ required: true, message: "请输入规格名称", trigger: "blur" }],
  };

  handleClose(tag, index, tagIndex) {
    let tempArr = this.classArr;
    console.log(tempArr[index].children[tagIndex].name.indexOf(tag));
    if (tempArr[index].children[tagIndex].name.indexOf(tag) === 0) {
      tempArr[index].children.splice(tagIndex, 1);
    }
    // tempArr[index].children.splice(, 1);
    this.$emit("changeClass", { type: "delSpecValue", list: tempArr });
  }

  showInput(index) {
    let tempArr = this.classArr;
    tempArr[index].inputVisble = true;
    this.classArr = tempArr;
    // this.$emit("changeClass", tempArr);
  }

  handleInputConfirm(index) {
    let tempArr = this.classArr;
    if (tempArr[index].inputValue) {
      tempArr[index].children.push({ name: tempArr[index].inputValue });
    }
    tempArr[index].inputVisble = false;
    tempArr[index].inputValue = "";
    this.$emit("changeClass", { type: "addSpecValue", list: tempArr });
  }
  addNewClass() {
    let tempArr = this.classArr;
    tempArr.push({
      inputValue: "",
      inputVisble: false,
      children: [],
      name: "",
      version: "",
    });
    this.$emit("changeClass", { type: "addSpec", list: tempArr });
  }
  delNormHandle(index) {
    this.$confirm("确认删除该规格?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        let tempArr = this.classArr;
        tempArr.splice(index, 1);
        this.$emit("changeClass", { type: "delSpec", list: tempArr });
        this.$message({
          message: "删除成功",
          type: "success",
        });
      })
      .catch(() => {
        this.$message({
          message: "删除失败",
          type: "error",
        });
      });
  }
  specSortHandle() {
    this.$emit("changeClass", { type: "sortSpecValue", list: this.classArr });
  }
}
</script>
<style scoped>
.norm__item__name--left {
  display: flex;
  align-items: center;
}
.norm__item__name--left > .el-form-item--small.el-form-item {
  margin: 0 !important;
}
</style>